﻿#include "Blood.h"
#include<qdebug.h>
void Blood::addColor(const qreal num, const QColor& Pen, const QColor& Brush)
{
	if (num<0|| num>1.0)
	{
		qWarning() << "输入的百分比错误:" << num; 
		return;
	}
	m_color[num]= BloodColor(Pen, Brush);
}

void Blood::draw(QPainter* painter)
{
	painter->save();
	painter->setPen(m_CurrentColor.PenColor);
	painter->drawRect(m_rect);
	painter->setBrush(m_CurrentColor.BrushColor);
	painter->drawRect(QRect(m_rect.topLeft(),QSize(m_rect.width()*m_num, m_rect.height())));
	painter->restore();
}

void Blood::draw(QPainter* painter, const qreal num)
{
	update(num);
	draw(painter);
}

void Blood::update(const qreal num)
{
	m_num = num;
	for (auto it= m_color.rbegin(); it!= m_color.rend(); it++)
	{
		/*qDebug() << m_num;
		qDebug() << it->first<< it->second.BrushColor;*/
		if (m_num>it->first)
		{
			m_CurrentColor = it->second;
			break;
		}
	}
}

void Blood::move(const QPoint& pos)
{
	m_rect.moveTo(pos);
}

void Blood::setRect(const QRect& rect)
{
	m_rect = rect;
}

void Blood::setSize(const QSize& size)
{
	m_rect.setSize(size);
}
